package controllers;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.validator.ValidatorException;

import dao.EmployeeDAORemote;
import dto.EmployeeDTO;

@ManagedBean
@SessionScoped
public class EmployeeController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	EmployeeDAORemote employeeDAO;

	@ManagedProperty(value = "#{employeeDTO}")
	EmployeeDTO employeeDTO;

	private UIComponent component;

	private Object value;

	private String messageClass;

	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}

	public void addEmployee(ActionEvent event) {
//		try {
//			FacesContext context = FacesContext.getCurrentInstance();
//			if (employeeDTO.getEmail() == null) {
//			} else if ((employeeDTO.getUsername().equals("")) || (employeeDTO.getUsername().equals(null))) {
//				context.addMessage(event.getComponent().getClientId(),
//						new FacesMessage("Please,enter a username!"));
//
//			} else if (employeeDAO.getEmployeeByEmailAndUsername(employeeDTO
//					.getEmail(), employeeDTO.getUsername()) != null) {
//				context.addMessage(event.getComponent().getClientId(),
//						new FacesMessage("The " + employeeDTO.getEmail() + " or " + employeeDTO.getUsername() + " already exists!"));
//
//			} else {
				employeeDAO.addEmployee(employeeDTO);
//			}
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//		}
	}
	
	public void validateEmail( ) throws ValidatorException {
		FacesContext context = FacesContext.getCurrentInstance();
		String email = (String) value;

      //Set the email pattern string
      Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
      //Match the given string with the pattern
      Matcher m = p.matcher(email);
      //check whether match is found
      boolean matchFound = m.matches();
      if (!matchFound)
//    	  context.addMessage(component.getClientId(),new FacesMessage("The email was send successfully !"));
//      else
    	  context.addMessage(component.getClientId(),new FacesMessage("The email address is invalid !"));
      setMessageClass("errorMessage");

  
	
}
	
	public String createEmployee(){
		FacesContext context = FacesContext.getCurrentInstance();
		if (employeeDTO.getName().equals("")){
			context.addMessage(null, new FacesMessage("Name required"));
			messageClass = "errorMessage";
		}
		
		if(employeeDTO.getUsername().equals("")){
			context.addMessage(null, new FacesMessage("Username required"));
			messageClass = "errorMessage";
		}
		
		if(employeeDTO.getEmail().equals("")){
			context.addMessage(null, new FacesMessage("Email required"));
			messageClass = "errorMessage";
		}
		
//		if(!employeeDTO.getEmail().equals("")){
//			validateEmail(context, component, value);
//		}
		if(!(employeeDTO.getPassword().equals("1234"))){
			context.addMessage(null, new FacesMessage("Please enter the default password"));
			messageClass = "notice";
		}
		
		if(context.getMessageList().size() > 0){
			return null;
		}
		else{
			ActionEvent event = null;
			addEmployee(event);
			return "Employee created";
		}
	}

	/**
	 * @param messageClass the messageClass to set
	 */
	public void setMessageClass(String messageClass) {
		this.messageClass = messageClass;
	}

	/**
	 * @return the messageClass
	 */
	public String getMessageClass() {
		return messageClass;
	}

}
